package com.sanji.sjmallwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.order.ShopcarChildBean;
import com.sanji.sjmallutils.Tools;

/**
 * Created by fhm on 2016/12/29.
 */

public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher {
    private int amount = 0;//购买数量
    private int goods_storage = 0;//商品库存
    private int goods_limit = 0;//商品限购
    private OnAmountChangeListener mListener;
    private ImageView reduceImage;//减少
    private ImageView addImage;//增加
    private EditText goodsNumberEdit;//显示数量
    private View view;


    public AmountView(Context context) {
        super(context);
    }

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.activity_goods_add_reduce_linear, this, true);
        reduceImage = (ImageView) view.findViewById(R.id.image_reduce);
        addImage = (ImageView) view.findViewById(R.id.image_add);
        goodsNumberEdit = (EditText) view.findViewById(R.id.edit_number_tv);
        reduceImage.setOnClickListener(this);
        addImage.setOnClickListener(this);
        goodsNumberEdit.addTextChangedListener(this);
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }

    /**
     * 设置商品属性
     *
     * @param goodsStorage 库存
     * @param goodsLimit   限购数量（-1表示不限购）
     */
    public void setGoodsStorage(int goodsStorage, int goodsLimit, int count) {
        this.goods_storage = goodsStorage;
        this.goods_limit = goodsLimit;
        this.goodsNumberEdit.setText(String.valueOf(count));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_reduce:
                //点击减少
                if (amount > 0) {
                    amount--;
                    goodsNumberEdit.setText(amount + "");
                } else {
                    Tools.toastTools(getContext(), "请选择商品");
                }
                break;
            case R.id.image_add:
                if ((goods_limit > 0 && amount < goods_limit) || (goods_limit < 0 && amount < goods_storage)) {
                    amount++;
                    goodsNumberEdit.setText(amount + "");
                } else {
                    Tools.toastTools(getContext(), "暂时没有货，请选择其他商品！");
                }
                break;
        }
        //        goodsNumberEdit.clearFocus();
        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty())
            return;
        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            goodsNumberEdit.setText(goods_storage + "");
            return;
        }

        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }

    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount);
    }

}
